<?php
$response=array();
require_once('db_config.php');
/* Open a connection */
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

$response['success']=1;

$query_loaihang="select maloai,tenloai from dmloaihang";
if ($stmt=$mysqli->query($query_loaihang)) 
{	
	$response["loaihangs"]=array();
	while ($row = $stmt->fetch_assoc())
	{
       	// temp user array
        $lh = array();
        $lh["maloai"] = $row["maloai"];
        $lh["tenloai"] = $row["tenloai"];  



        //LOAD ALL BANS
        $maloaihang=$row["maloai"];

        $maloaihang="'".$maloaihang."'";        

        $query_nhomhang="select manhom, tennhom, maquay  from dmnhomhang where maloai=$maloaihang";

        try 
        {        	
        	if($stmt_nh=$mysqli->query($query_nhomhang))
            {
                $lh["nhomhangs"]=array();

                while ($nhom =$stmt_nh->fetch_assoc())
                {
                    $b=array();
                    $b["manhom"]=$nhom["manhom"];
                    $b["tennhom"]=$nhom["tennhom"];
                    $b["maquay"]=$nhom["maquay"];

                    //load ds mat hangs
                    $manhom=$nhom["manhom"];

                    $manhom="'".$manhom."'";

                    $query_mathang="SELECT mahang,manguoidung,tendonvi,ngayca,combo,tenhang,dongia1,phantram,dongia2,dongia3,active,hetmon  FROM demohangban inner JOIN dmdonvitinh ON demohangban.madonvi=dmdonvitinh.madonvi where manhom=$manhom";

                    try
                    {
                        if($stmt_mh=$mysqli->query($query_mathang))
                        {
                            $b["mathangs"]=array();

                            //var_dump($stmt_mh);

                            while ($mathang=$stmt_mh->fetch_assoc())
                            {
                                $mh=array();
                                $mh["mahang"]=$mathang["mahang"];
                                $mh["manguoidung"]=$mathang["manguoidung"];
                                $mh["tendonvi"]=$mathang["tendonvi"];
                                $mh["tenhang"]=$mathang["tenhang"];
                                $mh["dongia1"]=$mathang["dongia1"];
                                $mh["dongia2"]=$mathang["dongia2"];
                                $mh["dongia3"]=$mathang["dongia3"];
                                $mh["active"]=$mathang["active"];
                                $mh["hetmon"]=$mathang["hetmon"];
                                $mh["ngayca"]=$mathang["ngayca"];
                                $mh["phantram"]=$mathang["phantram"];
                                $mh["combo"]=$mathang["combo"];

                                array_push($b["mathangs"], $mh);
                            }
                            $stmt_mh->close();
                        }

                    }
                    catch (Exception $e)
                    {
                        throw $e;
                    }

                    array_push($lh["nhomhangs"], $b);
                }
                $stmt_nh->close();
            }
        	
        } 
        catch (Exception $e) 
        {
           $response['success']=0;
           $response['errorcode']=$e->getMessage();
        }  
         // push single product into final response array
        array_push($response["loaihangs"], $lh);     
       
    }    
    $stmt->close();
    echo json_encode($response);
} 
$mysqli->close();
?>